/*
 * @description: 通用返回类
 * @Author: Islands
 * @Date: 2024-06-17 21:52:56
 * @LastEditTime: 2024-06-17 22:10:15
 */
import { ElNotification } from 'element-plus';
import { ElMessage } from 'element-plus'

//全局返回对象
export type IResult<T> = {
    flag: boolean,
    code: string,
    message: string,
    data: T,
}

/**
 * 通用响应提示
 * @param result 
 */
export const Result = (result: IResult<any>, cb: (arg?: any) => void) => {
    const { code, message, data } = result;
    switch (true) {
        case new RegExp("^20.*$").test(code):
            ElNotification.success({
                title: "成功", message: message
            })
            cb(data)
            break;
        case new RegExp("^40.*$").test(code):
            ElNotification.warning({
                title: "警告", message: message
            })
            break;
        case new RegExp("^50.*$").test(code):
            ElNotification.error({
                title: "错误", message: message
            })
            break;
        default:
            ElNotification.info({
                title: "未知错误", message: message
            })
            break;
    };
};



/**
 * 查询通用返回
 * @param result 
 * @returns 
 */
export const QResult = (result: IResult<any>, cb: (data: any) => void) => {
    const { code, message, data } = result;
    switch (true) {
        case new RegExp("^20.*$").test(code):
            cb(data)
            break;
        case new RegExp("^40.*$").test(code):
            ElMessage.warning({
                message
            })
            break;
        case new RegExp("^50.*$").test(code):
            ElMessage.error({
                message
            })
            break;
        default:
            ElMessage.info({
                message
            })
            break;
    };
}